package com.livery.app.view.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import com.livery.app.controller.AbstractWorkspaceController;
import com.livery.app.model.AbstractWorkspaceModel;
import com.livery.app.model.observators.ClientObserver;
import com.livery.data.Client;

public class ClientFrame extends JFrame implements ClientObserver, ActionListener{

	private AbstractWorkspaceController controller;
	private List<Client> list;
	private JTable tableClient;
	
	private JButton btnSearch;
	private JButton btnAddClient;
	private JButton btnEditClient;
	
	private JTextField fieldName;
	private JTextField fieldSurname;
	private JTextField fieldID;
	
	public ClientFrame(AbstractWorkspaceController controller){
		super("Klienci");
		this.controller = controller;
		this.setMinimumSize(new Dimension(800,600));
		this.addWindowListener	(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				setVisible(false);				
			}	
		});	

		Dimension dialogSize = getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		createView();
	}

	private void createView(){
		this.setLayout(new BorderLayout());
		this.add(createNorthPanel(), BorderLayout.NORTH);
		this.add(createSouthPanel(), BorderLayout.SOUTH);
		
	}
	private JPanel createNorthPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		JLabel labelName = new JLabel("Imię:", SwingConstants.RIGHT);
		JLabel labelSurname = new JLabel("Nazwisko:", SwingConstants.RIGHT);
		JLabel labelID = new JLabel("Nr dowodu:", SwingConstants.RIGHT);
		fieldName = new JTextField(10);
		fieldSurname = new JTextField(10);
		fieldID = new JTextField(10);
		btnSearch = createButton("Szukaj", "Wyszukaj klientów");
		panel.add(labelName);
		panel.add(fieldName);
		panel.add(labelSurname);
		panel.add(fieldSurname);
		panel.add(labelID);
		panel.add(fieldID);
		panel.add(btnSearch);
		return panel;
	}
	
	private JPanel createSouthPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		btnAddClient = createButton("Dodaj", "Dodaj nowego klienta");
		btnEditClient = createButton("Edytuj", "Edytuj aktualnie zaznaczonego klienta");
		panel.add(btnAddClient);
		panel.add(btnEditClient);
		return panel;
	}
	public void setDisabledButtonEdit(){
		btnEditClient.setEnabled(false);
	}
	private void setEnabledButtonEdit(){
		btnEditClient.setEnabled(true);
	}
	private void createCenterPanel(){
		JPanel panel = new JPanel();
		tableClient = new JTable();
	}
	private JButton createButton(String text, String toolTip){
		JButton button = new JButton(text);
		button.setToolTipText(toolTip);
		button.addActionListener(this);
		button.setEnabled(true);
		return button;
	}
	@Override
	public void updateClient(List<Client> list) {
		// TODO Auto-generated method stub
		this.list =  list;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == btnSearch){
			
		}
		if(e.getSource() == btnAddClient){
			
		}
		if(e.getSource() == btnEditClient){
			
		}
	}
}
